#ifndef __PRINTER_CPP__
#define __PRINTER_CPP__
#include "../include/all.h"


void Printer::printMenu(const wchar_t* toPrint) {
    system("cls");
    std::wcout << toPrint;
    system("pause");
}
std::wstring Printer::printMenu(
    const wchar_t* toPrint,
    std::set<std::wstring> menu) {

    system("cls");
    std::wcout << toPrint;
    std::wstring sel;
    while (
        std::getline(std::wcin, sel),
        !menu.contains(sel)) { // C++20 things

        system("CLS");
        std::wcout << toPrint;
    }
    return sel;
}

template <typename... T>
void Printer::fmtln(const wchar_t* fmtstr, T... args) {
    std::wcout << fmt::format(fmtstr, (args)...) << std::endl;
}


void selection(
    List<Student>* sList,
    List<Course>* cList,
    List<StuCrs>* scList) {

    Printer prt;
    std::wstring sel;
    prt.fmtln(L"{:^40s}", L"学生选课列表");
    std::vector<Node<Student>*> stuVec;
    std::vector<Node<Course>*> crsVec;
    std::vector<Node<StuCrs>*> scVec;
    for (auto p = sList->head; p->next; p = p->next) { stuVec.push_back(p); }
    for (auto p = cList->head; p->next; p = p->next) { crsVec.push_back(p); }
    for (auto p = scList->head; p->next; p = p->next) { scVec.push_back(p); }

    std::wcout << L"学生选课列表\n请输入要选课学生的学号: ";
    std::getline(std::wcin, sel);
    int sno = stoi(sel);

    Node<Student>* ps;
    bool exist = false;
    for (auto p : stuVec) {
        ps = p;
        if (p->next->data.sno == sno) {
            exist = true;
            break;
        }
    }
    if (!exist) {
        prt.fmtln(L"不存在学号为 {} 的学生.", sno);
        system("pause");
        return;
    }
    system("cls");
    prt.fmtln(L"学生信息: ");
    ps->next->data.print();

    // 根据学号在选课数据筛选课程号.
    auto scView = scVec | std::views::filter(
        [sno](Node<StuCrs>* p) {
            return p->next->data.sno == sno;
        }
    );
    // 根据课程号筛选课程 (已选).
    auto cView = crsVec | std::views::filter(
        [&scView](Node<Course>* pc) {
            for (auto const psc : scView) {
                if (pc->next->data.cno == psc->next->data.cno) {
                    return true;
                }
            }
            return false;
        }
    );
    // 根据课程号筛选课程 (未选).
    auto crView = crsVec | std::views::filter(
        [&scView](Node<Course>* pc) {
            for (auto const psc : scView) {
                if (pc->next->data.cno == psc->next->data.cno) {
                    return false;
                }
            }
            return true;
        }
    );

    prt.fmtln(L"已选课程:");
    for (auto p : cView) { p->next->data.print(); }

    prt.fmtln(L"可选课程:");
    for (auto p : crView) { p->next->data.print(); }

    system("pause");
    std::wcout << L"\n请输入课程号进行选课: ";
    std::getline(std::wcin, sel);

    int cno = stoi(sel);
    exist = false;
    for (auto p : crView) {
        if (p->next->data.cno == cno) {
            exist = true;
            break;
        }
    }
    if (exist) {
        Node<StuCrs>* pnew = new Node<StuCrs>;
        pnew->data.sno = sno;
        pnew->data.cno = cno;
        scList->tail->cat(pnew);
        scList->tail = pnew;
        prt.fmtln(L"选课成功!");
        system("pause");
    } else {
        prt.fmtln(L"课程不存在!");
        system("pause");
    }
}


#endif /* __PRINTER_CPP__ */
